package com.book.service.impl;

import com.book.dao.OrderDao;
import com.book.dto.OrderDto;
import com.book.dto.OrderPage;
import com.book.service.OrderService;
import com.book.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//错误码8001开始
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Override
    public ResponseResult getList(String userId, OrderPage orderPage) {
        try {
            PageHelper.startPage(orderPage.getCurrentPage(),orderPage.getPageSize());
            List<OrderDto> list = orderDao.getList(userId, orderPage.getOrderId(),
                    orderPage.getBookName(),orderPage.getStatus(),orderPage.getUserIdAdmin());//根据用户id查询，根据订单号和书名模糊查询，还有订单状态
            PageInfo<OrderDto> pageInfo = new PageInfo<>(list);

            if(orderPage.getCurrentPage()>pageInfo.getPages()){
                PageHelper.startPage(pageInfo.getPages(),orderPage.getPageSize());
                list = orderDao.getList(userId, orderPage.getOrderId(),
                        orderPage.getBookName(),orderPage.getStatus(),orderPage.getUserIdAdmin());//暂时只根据用户id查询，根据订单号和书名模糊查询
                pageInfo = new PageInfo<>(list);
            }

//            System.out.println(pageInfo);
            return new ResponseResult(200,"查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(8001,"获取订单失败");
        }
    }

    @Override
    public ResponseResult getOne(String orderId) {
        try {
            OrderDto orderDto = orderDao.getOne(orderId);
            return new ResponseResult(200,"获取成功",orderDto);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(8002,"获取此订单信息失败");
        }
    }

    @Override
    public ResponseResult updateStatus(String orderId, int status) {
        try {
            orderDao.updateStatus(orderId, status);
            return new ResponseResult(200,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(8003,"更新状态失败");
        }
    }

    @Override
    public ResponseResult deleteOrder(String orderId) {
        try {
            orderDao.deleteById(orderId);
            return new ResponseResult(200,"删除订单成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(8004,"删除订单失败");
        }
    }
}
